package controller;

import dao.RolePermissionDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * @version 1.0
 * @Author: 举个栗子吖
 * @Date: 2019-11-20 10:52
 */
@Controller
@RequestMapping("/rolePermission")
public class RolePermissionController {
    @Autowired
    private RolePermissionDao rolePermissionDao;

    @RequestMapping("/grantPermission")
    @ResponseBody
    public Map<String, Object> grantPermission(Integer roleid, String pmid) {
        Map<String, Object> map = new HashMap<String, Object>();
        String[] ss = pmid.split("-");
        int[] perids = new int[ss.length + 1];
        try {
            for (int i = 0; i < perids.length; i++) {
                perids[i] = Integer.parseInt(ss[i + 1]);

            }
            /*删除角色对应的权限*/
            rolePermissionDao.del(roleid);
            /*增加用户选择的权限*/
            for (int per : perids) {
                rolePermissionDao.save(roleid, per);
            }
            map.put("mssg", "1");
        } catch (NumberFormatException e) {
            map.put("mssg", "2");
            e.printStackTrace();
        }
        return map;
    }
}
